/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package org.moon.framework.sample.dynamicdatasource.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.moon.framework.autoconfigure.cache.CacheService;
import org.moon.framework.autoconfigure.config.SystemConfig;
import org.moon.framework.autoconfigure.datasource.annotation.DS;
import org.moon.framework.autoconfigure.springmvc.domain.MoonPage;
import org.moon.framework.autoconfigure.springmvc.response.R;
import org.moon.framework.autoconfigure.springmvc.response.RCode;
import org.moon.framework.autoconfigure.utils.SecurityUtils;
import org.moon.framework.autoconfigure.utils.ToolUtils;
import org.moon.framework.sample.dynamicdatasource.cache.UserCache;
import org.moon.framework.sample.dynamicdatasource.domain.query.UserQuery;
import org.moon.framework.sample.dynamicdatasource.entity.SysUserEntity;
import org.moon.framework.sample.dynamicdatasource.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 用户Controller
 * 
 * @author moon
 */
@RestController
@RequestMapping(value = "sysUser")
@Api(value="用户管理",tags="用户管理")
@Slf4j
public class SysUserController {
	
	@Autowired
	private SysUserService userService;
	@Autowired
	private CacheService cacheService;
	@Autowired
	private SystemConfig systemConfig;

	@PostMapping(value = "list")
	@ApiOperation(value="用户列表")
	public R<MoonPage<SysUserEntity>> list(@RequestBody UserQuery query) {
		IPage<SysUserEntity> page = userService.selectListPage(query);
		return R.success(MoonPage.of(page));
	}
	
	@PostMapping(value = "save")
	@ApiOperation(value="保存用户信息")
	@DS(value = "local")
	public R<String> save(@RequestBody SysUserEntity userEntity){
		if(ToolUtils.isEmpty(userEntity.getPhoneNumber())){
			return R.fail(RCode.PARAM_ERROR.getCode(),"手机号码不能为空");
		}
		//判断当前账号是否存在
		if(ToolUtils.isEmpty(userEntity.getId())){
			int count = userService.count(Wrappers.<SysUserEntity>lambdaQuery().eq(SysUserEntity::getPhoneNumber,userEntity.getPhoneNumber()));
			if(count>0){
				return R.fail(RCode.PARAM_ERROR.getCode(),"手机号码【"+userEntity.getPhoneNumber()+"】已存在");
			}
		}
		userEntity.setPassword(SecurityUtils.md5("123456"));
		userService.saveOrUpdate(userEntity);
		UserCache.cleanCache();
		return R.success();
	}

	@GetMapping(value = "getById")
	@ApiOperation(value="根据id获取用户")
	@DS(value = "local")
	public R<SysUserEntity> getById(Integer id) {
		return R.success(userService.getById(id));
	}

	@GetMapping(value = "removeById")
	@ApiOperation(value="根据id删除用户")
	public R<Void> removeById(Integer id) {
		userService.removeById(id);
		return R.success();
	}

	
	@GetMapping(value = "updateStatusById")
	@ApiOperation(value="根据id修改用户状态")
	public R<Void> updateStatusById(Integer id,Integer status) {
		userService.update(Wrappers.<SysUserEntity>lambdaUpdate().set(SysUserEntity::getStatus,status).eq(SysUserEntity::getId,id));
		return R.success();
	}

	@GetMapping("/cleanCache")
	@ApiIgnore
	public R<Void> cleanCache(){
		UserCache.cleanCache();
		return R.success();
	}
}